﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using CommonFunc;

public partial class Manageweb_ManageFile_ManageFile : System.Web.UI.Page
{
    MODEL.File MF = new MODEL.File();
    MODEL.File _MF = new MODEL.File();

    BLL.File BF = new BLL.File();

    protected void Page_Load(object sender, EventArgs e)
    {
        if (Request.Cookies["MasterID"] == null)
        {
            Response.Redirect("../Default.aspx");
        }

        ClickInit();
    }


    #region ClickInit [初始化]
    protected void ClickInit()
    {
        try
        {
            TABLE_Add_Mod.Visible = false;

            LB_ManageFile.Text = "";

            if (LB_ID.Text == "")
            {
                Btn_Submit.Click += new EventHandler(Btn_Add_Click);
            }
            else
            {
                Btn_Submit.Click += new EventHandler(Btn_Mod_Click);
            }
        }
        catch (Exception ex)
        {
            LB_ManageFile.Text = ex.Message;
        }
    }
    #endregion


    #region 表格数据初始化 [初始化]
    protected void TableDataInit()
    {
        try
        {
            if (LB_ID.Text == "")
            {
                TB_Title.Text = "";

                TB_Description.Text = "";

                HL_File.Visible = false;

                RFV_FU_File.Enabled = true;
            }
            else
            {
                MF.File_ID = Convert.ToInt32(LB_ID.Text);

                _MF = BF.GetDateByID(MF);

                TB_Title.Text = _MF.File_Title;

                TB_Description.Text = CommonFunction.CharReConvert(_MF.File_Description);

                HL_File.Visible = true;

                HL_File.NavigateUrl = "~/UploadFile/" + _MF.File_URL;

                RFV_FU_File.Enabled = false;
            }
        }
        catch (Exception ex)
        {
            LB_ManageFile.Text = ex.Message;
        }
    }
    #endregion


    #region 文件链接地址 [格式化]
    protected string FormatFileURL(object _FileURL)
    {
        return "~/UploadFile/" + _FileURL.ToString();
    }
    #endregion


    #region 添加文档 [GridView] [Command]
    protected void Btn_Add_Command(object sender, CommandEventArgs e)
    {
        try
        {
            TABLE_Add_Mod.Visible = true;

            LB_ID.Text = "";

            LB_TableTitle.Text = "添加文档";

            TableDataInit();
        }
        catch (Exception ex)
        {
            LB_ManageFile.Text = ex.Message;
        }
    }
    #endregion


    #region 修改文档描述信息 [GridView] [Command]
    protected void Btn_Mod_Command(object sender, CommandEventArgs e)
    {
        try
        {
            TABLE_Add_Mod.Visible = true;

            LB_ID.Text = e.CommandArgument.ToString();

            LB_TableTitle.Text = "修改文档";

            TableDataInit();
        }
        catch (Exception ex)
        {
            LB_ManageFile.Text = ex.Message;
        }
    }
    #endregion


    #region 删除文档 [GridView] [Command]
    protected void Btn_Del_Command(object sender, CommandEventArgs e)
    {
        try
        {
            MF.File_ID = Convert.ToInt32(e.CommandArgument);

            BF.Del(MF);

            GV_FileList.DataBind();

            LB_ManageFile.Text = "删除成功";
        }
        catch (Exception ex)
        {
            LB_ManageFile.Text = ex.Message;
        }
    }
    #endregion


    #region 添加文档 [Click]
    protected void Btn_Add_Click(object sender, EventArgs e)
    {
        try
        {
            MF.File_URL = CommonFunction.DateTimeRandomFileName() + CommonFunction.ReplaceStr2(FU_File.FileName);
            MF.File_Title = TB_Title.Text;
            MF.File_Description = CommonFunction.CharConvert(TB_Description.Text);
            MF.File_DateTime = DateTime.Now;

            FU_File.SaveAs(Server.MapPath("~/UploadFile/") + MF.File_URL);

            BF.Add(MF);

            GV_FileList.DataBind();

            LB_ManageFile.Text = "添加成功";
        }
        catch (Exception ex)
        {
            LB_ManageFile.Text = ex.Message;
        }
    }
    #endregion


    #region 修改文档 [Click]
    protected void Btn_Mod_Click(object sender, EventArgs e)
    {
        try
        {
            MF.File_ID = Convert.ToInt32(LB_ID.Text);
            MF.File_Title = TB_Title.Text;
            MF.File_Description = CommonFunction.CharConvert(TB_Description.Text);

            if (FU_File.HasFile)
            {
                MF.File_URL = CommonFunction.DateTimeRandomFileName() + 
                    CommonFunction.ReplaceStr2(FU_File.FileName);

                FU_File.SaveAs(Server.MapPath("~/UploadFile/") + MF.File_URL);

                BF.ModFileURLByID(MF);
            }

            BF.Mod(MF);

            GV_FileList.DataBind();

            LB_ManageFile.Text = "修改成功";

        }
        catch (Exception ex)
        {
            LB_ManageFile.Text = ex.Message;
        }
    }
    #endregion
}
